package com.details.app.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.details.app.Mapper.PropertyMapper;
import com.details.app.Mapper.PropertyValueMapper;
import com.details.app.Service.PropertyService;
import com.tamll.Type.DataType;
import com.tamll.Type.Parameter;
import com.tamll.bean.Property;
import com.tamll.bean.PropertyValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PropertyServiceImpl implements PropertyService {


    @Autowired
    private PropertyMapper propertyMapper;

    @Autowired
    private PropertyValueMapper propertyValueMapper;

    @Override
    public DataType selectPropertyCategoryId(Integer productId,Integer propertyCategoryId) {
        DataType dataType=new DataType();
        QueryWrapper<Property> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("property_category_id",propertyCategoryId);
//        查询到的相关key值
        List<Property> properties = propertyMapper.selectList(queryWrapper);
        List<Parameter> list=new ArrayList<>();
        for (Property property:properties) {
            try {
//           根据商品id和相关key值id 进行查询value值    property.getPropertyId(),
                PropertyValue propertyValues = selectPropertyvaluePropertyIdPropertyValuePropertyId(property.getPropertyId(), productId);
                Parameter parameter = new Parameter();
                parameter.setProperty_name(property.getPropertyName());
                parameter.setPropertyvalue(propertyValues.getPropertyValueValue());
                list.add(parameter);
            }catch (Exception e){
                continue;
            }
        }
        dataType.setParameterList(list);
        return dataType;
    }



    @Override
    public PropertyValue selectPropertyvaluePropertyIdPropertyValuePropertyId(Integer propertyvalue_property_id,Integer propertyvalue_product_id) {
        QueryWrapper<PropertyValue> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("propertyvalue_property_id",propertyvalue_property_id);
        queryWrapper.eq("propertyvalue_product_id",propertyvalue_product_id);
        return propertyValueMapper.selectList(queryWrapper).get(0);
    }

    /**
     * 判断有多少个value值
     * @return 返回结果
     */
    private Integer ifNum(Integer propertyvalue_product_id){
        QueryWrapper<PropertyValue> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("propertyvalue_product_id",propertyvalue_product_id);
        return propertyValueMapper.selectList(queryWrapper).size();
    }


}
